from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from login_ui import Ui_LoginDialog


class LoginDialog(QtWidgets.QDialog):
    def __init__(self, db_manager, parent=None):
        super().__init__(parent)
        self.ui = Ui_LoginDialog()
        self.ui.setupUi(self)
        self.db_manager = db_manager
        self.user_id = None

        # 连接信号槽
        self.ui.minimizeButton.clicked.connect(self.showMinimized)
        self.ui.closeButton.clicked.connect(self.close)
        self.ui.goToRegisterButton.clicked.connect(self.go_to_register)
        self.ui.goToLoginButton.clicked.connect(self.go_to_login)
        self.ui.loginButton.clicked.connect(self.login)
        self.ui.registerButton.clicked.connect(self.register)

        # 设置窗口无边框和可拖动
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.dragging = False
        self.offset = None

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragging = True
            self.offset = event.pos()

    def mouseMoveEvent(self, event):
        if self.dragging and self.offset is not None:
            self.move(self.pos() + event.pos() - self.offset)

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragging = False
            self.offset = None

    def go_to_register(self):
        self.ui.stackedWidget.setCurrentIndex(1)

    def go_to_login(self):
        self.ui.stackedWidget.setCurrentIndex(0)

    def login(self):
        username = self.ui.usernameLineEdit.text().strip()
        password = self.ui.passwordLineEdit.text().strip()

        if not username or not password:
            QtWidgets.QMessageBox.warning(self, "错误", "用户名和密码不能为空")
            return

        user_id = self.db_manager.authenticate_user(username, password)
        if user_id:
            self.user_id = user_id
            self.accept()  # 登录成功，关闭对话框并返回QDialog.Accepted
        else:
            QtWidgets.QMessageBox.warning(self, "错误", "用户名或密码错误")

    def register(self):
        username = self.ui.regUsernameLineEdit.text().strip()
        password = self.ui.regPasswordLineEdit.text().strip()
        confirm_password = self.ui.confirmPasswordLineEdit.text().strip()

        if not username or not password or not confirm_password:
            QtWidgets.QMessageBox.warning(self, "错误", "所有字段都必须填写")
            return

        if password != confirm_password:
            QtWidgets.QMessageBox.warning(self, "错误", "两次输入的密码不一致")
            return

        # 检查用户名是否已存在
        self.db_manager.cursor.execute("SELECT id FROM users WHERE username=%s", (username,))
        if self.db_manager.cursor.fetchone():
            QtWidgets.QMessageBox.warning(self, "错误", "用户名已存在")
            return

        # 创建新用户
        user_id = self.db_manager.create_user(username, password)
        if user_id:
            QtWidgets.QMessageBox.information(self, "成功", "注册成功！")
            self.ui.regUsernameLineEdit.clear()
            self.ui.regPasswordLineEdit.clear()
            self.ui.confirmPasswordLineEdit.clear()
            self.go_to_login()
        else:
            QtWidgets.QMessageBox.warning(self, "错误", "注册失败")